package com.example.prjbackend.mapper;

import com.example.prjbackend.domain.Shixisheng;
import com.example.prjbackend.domain.ShixishengDetail;
import com.example.prjbackend.domain.ShixishengQueryParam;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ShixishengMapper
{
    @Select("<script>" +
            "select s.sid,s.sname,s.sdescp,s.photoUrl,s.age,s.rz_date,s.b_id,b.bname bumengName " +
            "from shixisheng s left join bumeng b on s.b_id=b.bid " +
            "<where>" +
            // 仅当b_id不为null且不等于0时才拼接条件
            "<if test='b_id != null and b_id != 0'>and b_id=#{b_id} </if>" +
            // 仅当sname不为null且不为空字符串时才拼接条件
            "<if test='sname != null and sname != \"\"'>and sname like concat('%',#{sname},'%') </if>" +
            "<if test='sdescp != null and sdescp != \"\"'>and sdescp like concat('%',#{sdescp},'%') </if>" +
            "<if test='age1 != null and age2 != null'>and age between #{age1} and #{age2} </if>" +
            "</where>" +
            "</script>")
    List<ShixishengDetail> list(ShixishengQueryParam param);

    @Insert("insert into shixisheng(sname,sdescp,photoUrl,age,rz_date,b_id)" +
            "values(#{sname},#{sdescp},#{photoUrl},#{age},#{rz_date},#{b_id})")
    int add(Shixisheng shixisheng);

    @Update("<script>" +
            "update shixisheng " +
            "<set>" +
            "  <if test='sname!=null'>sname=#{sname},</if>" +
            "  <if test='sdescp!=null'>sdescp=#{sdescp},</if>" +
            "  <if test='photoUrl!=null'>photoUrl=#{photoUrl},</if>" +
            "  <if test='age!=null'>age=#{age},</if>" +
            "  <if test='rz_date!=null'>rz_date=#{rz_date},</if>" +
            "  <if test='b_id!=null'>b_id=#{b_id},</if>" +
            "</set>" +
            " where sid=#{sid}" +
            "</script>")
    int edit(Shixisheng shixisheng);

    @Delete("<script>" +
            "delete from shixisheng where sid in " +
            "<foreach collection='array' item='sid' open='(' close=')' separator=','>" +
            "#{sid}" +
            "</foreach>" +
            "</script>")
    int delete(Long[] ids);

}
